﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Xml;

namespace PictureResizerForms
{
    class dataTableBasedSettings
    {
        public const String COL_Name = "Name";
        public const String COL_Width = "Breite";
        public const String COL_Height = "Höhe";
        public const String COL_OutputPath = "Ausgabepfad";
        public const String TableName = "dtTemplatesMain";

        public System.Data.DataTable dt;

        /// <summary>
        /// Konstruktor, erzeugt die Datatable
        /// </summary>
        public dataTableBasedSettings()
        {
            dt = new DataTable(TableName);
            dt.Columns.Add(COL_Name);
            dt.Columns.Add(COL_Width);
            dt.Columns.Add(COL_Height);
            dt.Columns.Add(COL_OutputPath);  
        }

        /// <summary>
        /// Speichert die Datatable in einer XML Datei
        /// </summary>
        /// <param name="FileName"></param>
        public void Save(String FileName)
        {
            dt.WriteXml(FileName);
        }

        public void SaveXSD()
        {
            dt.WriteXmlSchema("c:\\dev\\xsd.xsd");
        }

        public void ValidateXML()
        {
            
        }

        /// <summary>
        /// Lädt die Datatable von einer XML Datei
        /// </summary>
        /// <param name="FileName"></param>
        public void Load(String FileName)
        {
            dt.Rows.Clear();
            dt.ReadXml(FileName);
        }

        /// <summary>
        /// Gibt eine Liste von Templates zurück
        /// </summary>
        /// <returns></returns>
        public List<String> GetTemplatesList()
        {
            List<String> returnList = new List<String>();

            foreach (DataRow row in dt.Rows)
            {
                returnList.Add(row.Field<String>(COL_Name));
            }
            return returnList;
        }

        public TemplateSettings getSettings(String templateName)
        {
            TemplateSettings actSettings = null;

            foreach (DataRow row in dt.Rows)
            {
                if(row.Field<String>(COL_Name).Equals(templateName))
                {
                    actSettings = new TemplateSettings()
                    {
                        height = row.Field<String>(COL_Height).ToString(),
                        width = row.Field<String>(COL_Width).ToString(),
                        outputPath = row.Field<String>(COL_OutputPath).ToString()
                    };
                }
            }

            return actSettings;
        }
    }
}
